package com.lpf;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import static com.lpf.util.CodeUtils.decodeUTF8Str;

public class Base64Python {

    private static final String pythonFuncFilePath = "E:\\IdeaProjects\\java-learning\\07-java-python\\src\\main\\resources\\apps\\base64\\main\\run.py";

    private static final String encryption = "encryption";
    private static final String decrypt = "decrypt";

    public static void main(String[] args) throws UnsupportedEncodingException {

        long startTime = System.currentTimeMillis();

        Properties props = new Properties();
        props.setProperty("python.home", "E:\\DevelopTools\\Python27");
        props.setProperty("python.console.encoding", "UTF-8");
        props.setProperty("python.options.showJavaExceptions", "true");
        props.setProperty("python.verbose", "warning");
        Properties preprops = System.getProperties();
        PySystemState.initialize(preprops, props);


        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile(pythonFuncFilePath);

        PyFunction pyf = interpreter.get(encryption, PyFunction.class);
        PyObject pyObject = pyf.__call__(Py.newStringUTF8("你在干嘛"));
        JSONObject jsonObject = JSONUtil.parseObj(pyObject);
        System.out.println("加密结果：" + jsonObject);

        pyf = interpreter.get(decrypt, PyFunction.class);
        pyObject = pyf.__call__(Py.newStringUTF8("5L2g5Zyo5bmy5Zib"));
        jsonObject = JSONUtil.parseObj(decodeUTF8Str(pyObject.toString()));
        System.out.println("解密结果：" + jsonObject);

        interpreter.cleanup();

        long endTime = System.currentTimeMillis();
        System.out.println("执行时间：" + (endTime - startTime));
    }

}
